package main

import (
	"bufio"
	"fmt"
	"net"
)

// socket server
func main() {

	listen, err := net.Listen("tcp", "127.0.0.1:10080")
	if err != nil {
		fmt.Println("net.listen err ", err)
		return
	}

	for {
		socket, err := listen.Accept()
		if err != nil {
			fmt.Println("listen accept err", err)
			continue
		}
		go processSocket(socket)
	}
}

func processSocket(socket net.Conn) {
	// 关闭连接
	defer socket.Close()

	for {
		reader := bufio.NewReader(socket)
		var buf [256]byte
		// 读取数据
		n, err := reader.Read(buf[:])
		if err != nil {
			fmt.Println("reader.Read  error : ", err)
			break
		}
		recvData := string(buf[:n])
		fmt.Println("receive data ：", recvData)
		fmt.Println("received data length ", n)
		if n < 256 {
			break
		}
	}

	// 响应客户端
	response := "this is server response >>>> "
	socket.Write([]byte(response))
}
